﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using chihuopig.Web.verfiy;
using chihuopig.IService;
using NLog;
using Microsoft.Extensions.Logging;
using chihuopig.Tool;
using System.Text;
using Microsoft.AspNetCore.Http;

namespace chihuopig.Web.Admin.Controllers
{
    public class LoginController : WebViewController
    {
        Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        private IAdminService _iAdminService;
        private readonly ILogger<LoginController> _logger;
        IHttpContextAccessor _IHttpContextAccessor;
        public LoginController
            (
                IAdminService iAdminService,
                ILogger<LoginController> Logger, 
                IHttpContextAccessor iHttpContextAccessor
            )
        {
            _iAdminService = iAdminService;
            _logger = Logger;
            _IHttpContextAccessor = iHttpContextAccessor;
        }

        public IActionResult LoginIndex()
        {

            //Logger.Info("普通信息日志");
           /* Logger.Debug("调试日志");
            Logger.Error("错误日志");
            Logger.Fatal("异常日志");
            Logger.Warn("警告日志");
            Logger.Trace("跟踪日志");*/
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Login(AdminLoginModel model)
        {
            if (ModelState.IsValid)
            {
                var cheklogin= _iAdminService.ChenkAdminLoginAsync(model.username,model.password);
                if (string.IsNullOrWhiteSpace(HttpContext.Session.GetString("GetIamgCode")) || HttpContext.Session.GetString("GetIamgCode")!= model.verificationcode)
                {
                    return Json(new JsonMsg() { succeed = false, msg = "验证码错误" });
                }
                if (await cheklogin!=null)
                {

                    string val= UtilityHelper.DESEncrypt(cheklogin.Result.Id.ToString(),CookieKeysCollection.Buffalo_Admin_Key, CookieKeysCollection.Buffalo_Admin_IV);
                    //UtilityHelper.SetCookie(CookieKeysCollection.Buffalo_Admin, Ase, DateTime.Now.AddDays(1));
                    CookieOptions cookie = new CookieOptions();
                    cookie.Expires = DateTime.Now.AddDays(1);
                    /*cookie.Path = "/";
                    cookie.Domain= _IHttpContextAccessor.HttpContext.Request.Host.Value;*/
                    _IHttpContextAccessor.HttpContext.Response.Cookies.Append(CookieKeysCollection.Buffalo_Admin, val, cookie);
                    HttpContext.Session.SetString("GetIamgCode", "");
                    return Json(new JsonMsg() { succeed = true });
                }
                else
                {
                    return Json(new JsonMsg() { succeed = false, msg="用户名或密码错误"});
                }
                
            }
            return Json(ModelStateVefify.Vefifymsg(ModelState));
           
        }

        public IActionResult GetIamgCode()
        {
            string code = string.Empty;
            var file = UtilityHelper.GetMemoryCode(out code);
            //Logger.Info("普通信息日志");
            /* Logger.Debug("调试日志");
             Logger.Error("错误日志");
             Logger.Fatal("异常日志");
             Logger.Warn("警告日志");
             Logger.Trace("跟踪日志");*/
            HttpContext.Session.SetString("GetIamgCode", code);
            return File(file.ToArray(), "image/png");
        }
    }
}